<?php

namespace app\admin\controller;

use app\admin\model\RoleUser as RoleUserModal;
use app\admin\validate\RoleUser as RoleUserValidate;
use app\BaseController;
use think\facade\Request as faRequest;

class RoleUser extends BaseController
{
    protected $middleware = ['Auth'];

    public function list() {
        $params = $this->request->post(['username', 'current', 'size']);
        $whereArr = [];
        if(isset($params['username']) && $params['username']) {
            $whereArr[] = ["username", "LIKE", '%'.$params['username'].'%'];
        }
        $sql = new RoleUserModal();
        $list = $sql->where($whereArr)
            ->order('create_at', 'desc')
            ->paginate([
                'list_rows'=> $params['size'],
                'page' => $params['current'],
            ]);;
        return result()->success($list);
    }

    public function add() {
        $params = faRequest::post(['mentId','phone', 'username', 'nickname', 'avatar', 'email',  'password']);
        $validate = new RoleUserValidate();
        if (!$validate->scene('add')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $params['password'] = md5($params['password']);
        $sql = new RoleUserModal();
        $sql->save($params);
        return result()->success("添加成功");
    }

    public function password() {
        $params = faRequest::post(['id','password']);
        $validate = new RoleUserValidate();
        if (!$validate->scene('edit')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $params['password'] = md5($params['password']);
        $sql = new RoleUserModal();
        $info = $sql->find($params['id']);
        $info->save($params);
        return result()->success("修改成功");
    }

    public function edit() {
        $params = faRequest::post(['id', 'mentId','avatar', 'username', 'nickname', 'photo', 'email']);
        $validate = new RoleUserValidate();
        if (!$validate->scene('edit')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new RoleUserModal();
        $info = $sql->find($params['id']);
        $info->save($params);
        return result()->success("修改成功");
    }

    public function del() {
        $params = faRequest::post(['id']);
        $validate = new RoleUserValidate();
        if (!$validate->scene('del')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new RoleUserModal();
        $sql->destroy($params['id']);
        return result()->success("删除成功");
    }

    public function dels() {
        $params = faRequest::post(['ids']);
        $validate = new RoleUserValidate();
        if (!$validate->scene('dels')->check($params)) {
            return result()->fail($validate->getError(), 500);
        }
        $sql = new RoleUserModal();
        $sql->destroy($params['ids']);
        return result()->success("删除成功");
    }
}